package com.htgd.gardenexpo.service.impl;

import com.htgd.gardenexpo.dao.GeConReservationInformationDao;
import com.htgd.gardenexpo.dto.GeConReservationDTO;
import com.htgd.gardenexpo.entity.GeConReservation;
import com.htgd.gardenexpo.dao.GeConReservationDao;
import com.htgd.gardenexpo.entity.GeConReservationInformation;
import com.htgd.gardenexpo.entity.GeMyInformation;
import com.htgd.gardenexpo.entity.PageQuery;
import com.htgd.gardenexpo.service.GeConReservationInformationService;
import com.htgd.gardenexpo.service.GeConReservationService;
import com.htgd.gardenexpo.service.GeMyInformationService;
import com.htgd.gardenexpo.service.WxUserService;
import com.htgd.gardenexpo.vo.ConstInformatin;
import com.htgd.gardenexpo.vo.ReservationDetails;
import com.htgd.gardenexpo.vo.ReserveList;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 预约入园(GeConReservation)表服务实现类
 *
 * @author makejava
 * @since 2021-06-21 17:55:02
 */
@Service("geConReservationService")
public class GeConReservationServiceImpl implements GeConReservationService {
    @Resource
    private GeConReservationDao geConReservationDao;

    @Autowired
    private WxUserService wxUserService;

    @Autowired
    private GeConReservationInformationDao geConReservationInformationDao;

    @Autowired
    private GeConReservationInformationService geConReservationInformationService;

    @Autowired
    private GeMyInformationService geMyInformationService;

    /**
     * 通过ID查询单条数据
     *
     * @param reservationId 主键
     * @return 实例对象
     */
    @Override
    public GeConReservation queryById(Integer reservationId) {
        return this.geConReservationDao.queryById(reservationId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<GeConReservation> queryAllByLimit(int offset, int limit) {
        return this.geConReservationDao.queryAllByLimit(offset, limit);
    }

    @Override
    public List<Map<String,Object>> getDataByDay(String nowDay) {
        return this.geConReservationDao.getDataByDay(nowDay);
    }
    @Override
    public List<Map<String,Object>> getDataByWeek(String monday,String sunday) {
        return this.geConReservationDao.getDataByWeek(monday,sunday);
    }
    @Override
    public List<Map<String,Object>> getDataByMonth(String firstDay,String lastDay) {
        return this.geConReservationDao.getDataByMonth(firstDay,lastDay);
    }

    @Override
    public List<GeConReservation> queryAllReservationsByValue(PageQuery pageQuery) {
        return this.geConReservationDao.queryAllReservationsByValue(pageQuery);
    }

    @Override
    public List<GeConReservation> getInfoByValue(String openId, String date, Integer venueId) {
        return this.geConReservationDao.getInfoByValue(openId,date,venueId);
    }

    /**
     * 新增数据
     *
     * @param geConReservation 实例对象
     * @return 实例对象
     */
    @Override
    public GeConReservation insert(GeConReservation geConReservation) {
        this.geConReservationDao.insert(geConReservation);
        return geConReservation;
    }

    /**
     * 修改数据
     *
     * @param geConReservation 实例对象
     * @return 实例对象
     */
    @Override
    public GeConReservation update(GeConReservation geConReservation) {
        this.geConReservationDao.update(geConReservation);
        return this.queryById(geConReservation.getReservationId());
    }

    /**
     * 通过主键删除数据
     *
     * @param reservationId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer reservationId) {
        return this.geConReservationDao.deleteById(reservationId) > 0;
    }

    @Override
    public String addreserve(GeConReservationDTO geConReservationDTO, String openId) {
        GeConReservation geConReservation = new GeConReservation();
        BeanUtils.copyProperties(geConReservationDTO,geConReservation);

        String[] split = geConReservationDTO.getInformationIds().split(",");
        //手机号
//        判断手机号是不是空
//        boolean flag = wxUserService.judgePhoneIsNullByOpenId(openId);
//        if(!flag){
//            return null;
//        }
//        String phone = wxUserService.getPhoneByOpenId(openId);
//        if(phone!=null){
//            geConReservation.setPhone(phone);
//        }else {
//            geConReservation.setPhone("");
//        }
        geConReservation.setPhone("");

        //昵称
        String name = wxUserService.getNameByOpenId(openId);
        if(name!=null){
            geConReservation.setUserName(name);
        }else {
            geConReservation.setUserName("");
        }
        geConReservation.setUserId(openId);
        geConReservation.setAccompanyNum(split.length);
        geConReservation.setStatus("1");
        geConReservationDao.insert(geConReservation);
        for (String informationId : split) {
            GeConReservationInformation geConReservationInformation = new GeConReservationInformation();
            geConReservationInformation.setReservationId(geConReservation.getReservationId());
            geConReservationInformation.setInformationId(Integer.valueOf(informationId));
            geConReservationInformationDao.insert(geConReservationInformation);
        }
        return geConReservation.getReservationId().toString();
    }

    @Override
    public ReservationDetails reservedetails(int reservationId) {
        ReservationDetails reservationDetails = new ReservationDetails();
        List<ConstInformatin> constInformatins = new ArrayList<>();

        GeConReservation geConReservation = geConReservationDao.queryById(reservationId);
        BeanUtils.copyProperties(geConReservation,reservationDetails);

        List<GeConReservationInformation> geConReservationInformations = geConReservationInformationService.getInformations(reservationId);
        for (GeConReservationInformation geConReservationInformation : geConReservationInformations) {
            ConstInformatin constInformatin = new ConstInformatin();
            GeMyInformation geMyInformation = geMyInformationService.getById(geConReservationInformation.getInformationId());
            BeanUtils.copyProperties(geMyInformation,constInformatin);
            constInformatins.add(constInformatin);
        }
        reservationDetails.setInformatins(constInformatins);
        return reservationDetails;
    }

    @Override
    public List<ReserveList> reservelist(PageQuery pageQuery, String openId) {
        return geConReservationDao.reservelist(pageQuery,openId);
    }

    @Override
    public long getTotalByDate(String date, Integer venueId) {
        return geConReservationDao.getTotalByDate(date,venueId);
    }
}
